package com.fangmao.a18_rxjava;

import android.os.Bundle;
import android.widget.TextView;

import com.fangmao.a18_rxjava.network.Api;

import java.util.concurrent.TimeUnit;

import androidx.annotation.MainThread;
import androidx.appcompat.app.AppCompatActivity;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.Single;
import io.reactivex.SingleObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.internal.operators.observable.ObservableInterval;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    private Disposable mDisposable;
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = findViewById(R.id.textview);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.github.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

        Api api = retrofit.create(Api.class);
//        api.getRepos("rengwuxian")
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new SingleObserver<List<Repo>>() {
//                    @Override
//                    public void onSubscribe(Disposable d) {
//                        mDisposable = d;
//                        mTextView.setText("正在请求...");
//                    }
//
//                    @Override
//                    public void onSuccess(List<Repo> repos) {
//                        mTextView.setText(repos.get(0).getName());
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        String error = e.getMessage();
//                        if (error == null) {
//                            error = e.getClass().getName();
//                        }
//                        mTextView.setText(error);
//                    }
//                });

        //---------------------------
        Observable.interval()
        Observable.just("1", "2")
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(String s) {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

        Single.just(1)
                .map(new Function<Integer, String>() {
                    @Override
                    public String apply(Integer integer) throws Exception {
                        return String.valueOf(integer);
                    }
                })
                .map(new Function<String, String>() {
                    @Override
                    public String apply(String str) throws Exception {
                        return String.valueOf(str + " wman");
                    }
                })
                .delay(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SingleObserver<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onSuccess(String s) {
                        mTextView.setText(s);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }
                });
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        //丢弃/取消
        mDisposable.dispose();
    }
}
